<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode==='show'" ref="dialogForm" label-width="100px" label-position="right">
			<el-form-item label="数据库厂商" prop="dbType">
				<sc-select v-model="form.dbType" dict="db_type" placeholder="请选择数据库厂商" style="width: 100%;" clearable></sc-select>
			</el-form-item>
			<el-form-item label="字段类型" prop="columnType">
				<el-input v-model="form.columnType" placeholder="请输入字段类型" clearable></el-input>
			</el-form-item>
			<el-form-item label="Java类型" prop="javaType">
				<el-input v-model="form.javaType" placeholder="请输入Java类型" clearable></el-input>
			</el-form-item>
			<el-form-item label="Java包名" prop="javaTypePackage">
				<el-input v-model="form.javaTypePackage" placeholder="请输入Java包名" clearable></el-input>
			</el-form-item>
			<el-form-item label="页面组件" prop="htmlType">
				<sc-select v-model="form.htmlType" dict="html_type" placeholder="请选择页面组件" style="width: 100%;" clearable></sc-select>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!=='show'" type="primary" :loading="isSaving" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			// 表单元素控制
			mode: "add",
			titleMap: {
				add: '新增',
				edit: '编辑',
				show: '查看'
			},
			visible: false,
			isSaving: false,
			// 表单数据
			form: {
				id: null,
				columnType: null,
				dbType: null,
				javaType: null,
				javaTypePackage: null,
				htmlType: null
			},
			// 表单验证规则
			rules: {
				dbType: [
					{required: true, message: '请选择数据库厂商', trigger: 'blur'}
				],
				columnType: [
					{required: true, message: '请输入字段类型', trigger: "blur"}
				],
				javaType: [
					{required: true, message: '请输入Java类型', trigger: 'blur'}
				],
				javaTypePackage: [
					{required: true, message: '请输入Java包名', trigger: 'blur'}
				],
				htmlType: [
					{required: true, message: '请选择页面组件', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		// 显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		// 表单注入数据
		setData(data) {
			this.form.id = data.id;
			this.form.columnType = data.columnType;
			this.form.dbType = data.dbType;
			this.form.javaType = data.javaType;
			this.form.javaTypePackage = data.javaTypePackage;
			this.form.htmlType = data.htmlType;
		},
		// 表单提交
		submit() {
			this.$refs.dialogForm.validate((valid) => {
				if (valid) {
					this.isSaving = true;
					if (this.form.id) {
						this.$API.gen.columnMap.edit.put(this.form).then(() => {
							this.$emit('success');
							this.visible = false;
							this.$message.success("保存成功");
						}).catch(() => {
							this.isSaving = false;
						});
					} else {
						this.$API.gen.columnMap.add.post(this.form).then(() => {
							this.$emit('success');
							this.visible = false;
							this.$message.success("保存成功");
						}).catch(() => {
							this.isSaving = false;
						});
					}
				}
			})
		}
	}
}
</script>

<style>
</style>
